import 'package:flutter/material.dart';
import 'package:interview/components/app/app_text_button.dart';
import 'package:interview/components/page/page_nav_bar.dart';
import 'package:interview/components/status_bar_warpper.dart';
import 'package:interview/config/task_queue.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/utils/logger.dart';

class TaskPage extends StatefulWidget {
  const TaskPage({super.key});

  @override
  State<TaskPage> createState() => _TaskPageState();
}

class _TaskPageState extends State<TaskPage> {
  late TaskQueue taskQueue;

  @override
  void initState() {
    super.initState();
    taskQueue = TaskQueue();
    taskQueue.updateMaxConcurrentTasks(); // 初始化并更新最大并发数
  }

  // 模拟任务执行逻辑
  Future<void> _mockTask(String taskName, int durationInSeconds) async {
    Logger.info("开始执行任务：$taskName", tag: "_mockTask");
    await Future.delayed(Duration(seconds: durationInSeconds));
    Logger.info("执行任务完成：$taskName", tag: "_mockTask");
  }

  // 任务池执行
  void _processQueue() {
    taskQueue.processQueue();
  }

  // 添加低优先级任务
  void _addLowPriorityTask() {
    final task = Task(3, () => _mockTask("低优先级任务", 3) , "低优先级任务");
    taskQueue.addTask(task);
    Logger.info("已添加低优先级任务" ,  tag: "_addLowPriorityTask");
  }

  // 添加中优先级任务
  void _addMediumPriorityTask() {
    final task = Task(2, () => _mockTask("中优先级任务", 3) , "中优先级任务");
    taskQueue.addTask(task);
    Logger.info("已添加中优先级任务" , tag: "_addMediumPriorityTask");
  }

  // 添加高优先级任务
  void _addHighPriorityTask() {
    final task = Task(1, () => _mockTask("高优先级任务", 3),"高优先级任务");
    taskQueue.addTask(task);
    Logger.info("已添加高优先级任务" ,tag: "_addHighPriorityTask" );
  }

  // 测试动态优先级提升
  void _testPriorityBoosting() {
    // 添加多个低优先级任务，观察是否随着等待时间自动提升优先级
    final task1 = Task(3, () => _mockTask("低优先级任务", 3),"低优先级任务");
    final task2 = Task(2, () => _mockTask("中优先级任务", 3),"中优先级任务");
    final task3 = Task(1, () => _mockTask("高优先级任务 ", 3),"高优先级任务");
    taskQueue.addTask(task1);
    taskQueue.addTask(task2);
    taskQueue.addTask(task3);
    
  }

  // 测试动态并发调整
  Future<void> _testConcurrencyAdjustment() async {
    await taskQueue.updateMaxConcurrentTasks();
    Logger.info("已测试动态并发调整" , tag: "_testConcurrencyAdjustment");
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;
    return StatusBarWrapper(
      statusBarColor: theme.subjectPure!,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark? Brightness.light: Brightness.dark,
      child: Scaffold(
        appBar: null,
        body: SafeArea(
          child: Column(
            children: [
              PageNavBar(
                theme: theme,
                showTitle: true,
                title: "多线程任务",
                showRight: false,
              ),
              // 测试多线程任务的按钮
              AppTextButton(
                text: "执行任务",
                onPressed: _processQueue,
              ),
              AppTextButton(
                text: "添加1条低优先级任务",
                onPressed: _addLowPriorityTask,
              ),
              AppTextButton(
                text: "添加1条中优先级任务",
                onPressed: _addMediumPriorityTask,
              ),
              AppTextButton(
                text: "添加1条高优先级任务",
                onPressed: _addHighPriorityTask,
              ),
              AppTextButton(
                text: "测试动态优先级提升",
                onPressed: _testPriorityBoosting,
              ),
              AppTextButton(
                text: "测试动态并发调整",
                onPressed: _testConcurrencyAdjustment,
              ),
            ],
          ),
        ),
      ),
    );
  }
}